<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_login
 *
 * This is an M_login of a few basic login interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Login extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_login');
		$this->load->model('mdl_apps');
	}
/*	
	function login_get()
    {
        
		
		$rows = array();
		$login = array();
		
		
		$page=$this->get('page');
		$limit=$this->get('lim');
		$key=$this->get('key');
		$value=$this->get('val');
		$countitem = 0;
		if (($key!=null) && ($page!=null) ){
			
			$arrwhere=$key." LIKE '%".$value."%'";
			$query = $this->mdl_login->get_item($limit, $page, $arrwhere);
			$countitem = $this->mdl_login->get_countitem($arrwhere);
		}elseif($limit!=null){
		
			$query = $this->mdl_login->get_item($limit, $page);
			$countitem = $this->mdl_login->get_countitem();
		}else{
			$query = $this->mdl_login->get_item();
			$countitem = $this->mdl_login->get_countitem();
		}
		
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['userid'] = $row['userid'];
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['level_id'] = $row['level_id'];

				$i++;
			}
			$data = array(
				'countitem' => $countitem,
				'datas' => $rows
				
			);
		}
		
		//print_r($data);
		
        if($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Items could not be found'), 404);
        } 
    }
    */
   /* function login_post()
    {
        //$this->some_model->updatelogin( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_login->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function check_login_post(){
		       //$this->some_model->updatelogin( $this->get('id') );
        
		//post
		
		$username=$this->post('username'); 
		$password=$this->post('password'); 
		$appsidonapps=$this->post('appsidonapps'); 
		$ip=$this->post('ip'); 
		$device=$this->post('device');
		$os=$this->post('os'); 
		$location=$this->post('location'); 
		
		// check login, firsttime, verification, pin
		
		$encrypt=md5($password);
		$arrwhere="username = '" . $username . "' and password = '" . $encrypt . "'";
		$query = $this->mdl_login->get_item(0, 0, $arrwhere);
		$data = '';
		$logincheck = 0;
		$firstimecheck = 0;
		$verificationcheck=0;
		$pincheck = 0;
		$appsidonappscheck = 0;
				
		$loginstatus=0;
		$firstimestatus = 0;
		$verificationstatus=0;
		$pinstatus = 'false';
		$pinnotification = 'false';
		$appsidonserver = '';
		$appsver = '';
		$appsidstatus = 'true';
		$updateappsidstatus ='false';
		$verificationstatus = 'false';
		$verificationnotification = 'false';
		
		// --- start pengecekan login
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['firsttimestatus'] = $row['firsttimestatus'];
				$rows[$i]['verificationstatus'] = $row['verificationstatus'];
				$rows[$i]['pinstatus'] = $row['pinstatus'];
				$rows[$i]['appsver'] = $row['appsver'];
				

				$i++;
			}
		
			$logincheck = 1;
			$firstimestatus = $rows[0]['firsttimestatus'];
			$verificationstatus = $rows[0]['verificationstatus'];
			$pinstatus = $rows[0]['pinstatus'];
			$appsver = $rows[0]['appsver'];
			
			
		
		}
		// --- end pengecekan login
		
			// login status
	
			if ($logincheck){
			//$this->response(array('status' => 'true','data' => $data),200);
			$loginstatus = 'true';
			$loginnotification = 'false';
			
			// --- start ceks apps
			
			
			
		$arrwhere="appsid = '" . $appsidonapps .  "'";
		$query = $this->mdl_apps->get_item(0, 0, $arrwhere);
		
		
		
		// cek appsid dari apps
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['appsid'] = $row['appsid'];
				$i++;
			}
			$data = array(
				'datas' => $rows
			);
			
			$appsidonappscheck = 1;
		
			
			$appsidstatus = 'true';
			$updateappsidstatus ='false';

			$debug = 'apps id ada di tapps; ';
			$appsid = $appsidonapps;
		}
		
			// kalau appsid dari apps ada di tapps cek ke tusers
		if ($appsidonappscheck){
			$arrwhere="appsid = '" . $appsidonapps . "' and username ='" .$username. "'";
		$query = $this->mdl_login->get_item(0, 0, $arrwhere);
		$data = '';
		$appsidonservercheck = 0;
		
		// cek appsid dari apps ke t usrs
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				//$rows[$i]['appsid'] = $row['appsid'];
				$rows[$i]['username'] = $row['username'];
				
				
				$i++;
			}
			
			$appsidonservercheck = 1;
			//$appsidonserver = $rows[0]['appsid'];
			
			$data = array(
				'ip' => $ip,
				'device' => $device,
				'os' => $os,
				'location' => $location
				
			);

			$appsidstatus = 'true';
			$updateappsidstatus ='false';

			$query = $this->mdl_login->updateip($username,$data);
		
			$debug = ' appsid ada di tapps, cocok dg username di tuser';
			
		} else {
		$appsidstatus = 'true';
		$updateappsidstatus ='false';
		$query = $this->mdl_login->updateappsid($username,$appsidonapps);
		$debug = ' appsid ada di tapps, ngak cocok dg username di tuser, update appsid di tuser';
	
		} 
			
		}else{
			$appsidstatus = 'true';
			$updateappsidstatus ='true';
			$debug = ' appsid ngak ada di tapps atau null, bikin appsid baru di tapps, update appsid di tuser';
	
			$query = $this->mdl_apps->insert();
			$query = $this->mdl_apps->get_lastitem(0, 0);
	$i=0;
			foreach($query->result_array() as $row)
			{
				
				$rows[$i]['appsid'] = $row['appsid'];
				
				$i++;
			}
			$appsid = $rows[0]['appsid'];
			$query = $this->mdl_login->updateappsid($username,$appsid);
			$appsidonapps = $appsid;
		
		
		}
		
			
			
			// ----- end ceks apps
			
			
				// verification status	
			
			if ($verificationstatus == 1){
			$verificationstatus = 'true';
			$verificationnotification = 'false';
			} else {
			$verificationstatus = 'false';
			$verificationnotification = 'Verifikasi akun anda belum dilakukan. Silahkan cek email anda untuk klik link verifikasi.';
			}
			
			
						// pin status
			
			if ($pinstatus == 1){
			$pinstatus = 'true';
			$pinnotification = 'false';
			
			}else{
			$pinstatus = 'false';
			$pinnotification = 'belun terdaftar';
			
			}
			
			
			}else{
			$loginstatus = 'false';
			$loginnotification = 'Login anda gagal';
			$appsstatus = 'false';
			$debug = '  appsid ngak ada di tapps atau null, login false';
	
			$appsidonapps = 'false';
			
			}
		
			// firsttime status
			
			if ($firstimestatus == 1){
			$firstimestatus = 'true';
			}else{
			$firstimestatus = 'false';
			}

		
			

			// apps version
			
			if ($appsver == ''){
			$appsver = 'false';
			}
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['password'] = $password;
			$token['appsidonapps'] = $appsidonapps;
			$token=JWT::encode($token, 'dompeto');
			$resp ='login';
		
		$this->response(array( 'resp' => $resp ,'token' => $token ,'loginstatus' => $loginstatus ,'loginnotification' => $loginnotification , 'updateappsidstatus' => $updateappsidstatus, 'appsidstatus' => $appsidstatus , 'appsidonapps' => $appsidonapps , 'firstimestatus' => $firstimestatus, 'verificationstatus' => $verificationstatus,  'verificationnotification' => $verificationnotification ,'pinstatus' => $pinstatus,'pinnotification' => $pinnotification,  'appsver' => $appsver, 'debug' => $debug));
	}
	
	private function generate_token($id) {
    $key = hash_hmac('sha512', $id, $this->secret_key);
    $token = hash_hmac('sha512', $id, $key);

    return $token;
}
    /*
    function login_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_login->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function login_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_login->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function logins_get()
    {
        //$logins = $this->some_model->getSomething( $this->get('limit') );
        $logins = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($logins)
        {
            $this->response($logins, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
}